package com.chinasofti.framework.dataaccess;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.chinasofti.framework.base.Utils;
import com.chinasofti.framework.data.Entity;
import com.chinasofti.framework.net.HttpRequestClient;
import com.chinasofti.framework.net.Request;
import com.chinasofti.framework.net.Response;
import com.chinasofti.framework.net.ResponseDataCategory;
import com.chinasofti.framework.net.ResponseParser;
import com.chinasofti.framework.parser.XmlParser;

public abstract class ServerDataAccess<T extends Entity> implements IDataAccess<T>{

	protected abstract String getUrl();

	public ServerDataAccess() {
	
	}
	
	@Override
	public void loadAll(ArrayList<T> outEntities) throws Exception {
		Request<T> request = new Request<T>();
		String outString = null;
		HttpRequestClient.getInstance().post(getUrl(), request.toString(), outString);
		ResponseParser<T> parser = new ResponseParser<T>();
		Response<T> response = new Response<T>();
		parser.DoParse(outString, response);
		outEntities = response.getEntities();
	}

	@Override
	public void load(Object key, T outEntity) throws Exception {
		Request<T> request = new Request<T>(key);
		String outString = null;
		HttpRequestClient.getInstance().post(getUrl(), request.toString(), outString);
		ResponseParser<T> parser = new ResponseParser<T>();
		Response<T> response = new Response<T>();
		parser.DoParse(outString, response);
		ArrayList<T> outEntities = response.getEntities();
		if(outEntities != null && outEntities.size() != 0) {
			outEntity = outEntities.get(0);
		}
	}

	@Override
	public void load(Filter filter, ArrayList<T> outEntities) throws Exception {
		Request<T> request = new Request<T>(filter);
		String outString = null;
		HttpRequestClient.getInstance().post(getUrl(), request.toString(), outString);
	}

	@Override
	public void load(ArrayList<Parameter> params, ArrayList<T> outEntities) 
			throws Exception {
		ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
		
		for(Parameter param : params) {
			String name = param.getName();
			Object value = param.getValue();
			String valueStr = Utils.getSimpleString(value);
			NameValuePair nameValuePair = new BasicNameValuePair(name, valueStr);
			pairs.add(nameValuePair);
		}
		
		StringBuilder buffer = new StringBuilder(); 
		HttpRequestClient.getInstance().post(getUrl(), pairs, buffer);
		Response<T> response = new Response<T>();
		XmlParser<Response<T>> responseParser = response.getParser();
		response.set("Data", outEntities);
		responseParser.DoParse(buffer.toString(), response);
	}

	@Override
	public long loadAll(long from, long to, ArrayList<T> entities,
			ArrayList<ResponseDataCategory> outCategories) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long load(Filter filter, long from, long to,
			ArrayList<T> outEntities,
			ArrayList<ResponseDataCategory> outCategories) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long load(ArrayList<Parameter> params, long from, long to,
			ArrayList<T> outEntities,
			ArrayList<ResponseDataCategory> categories) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean save(T entity) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean save(ArrayList<T> entities) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Object key) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
}
